import Vue from 'vue'
import App from './App'
import router from './router'
import Vant from 'vant'
import mui from 'vue-awesome-mui'
import wxLogin from 'weixin-js-sdk'
import { CONFIG } from './config/index.js'
import store from './store'   
import './config/rem'
import Qs from 'qs' 
import VueClipboard from 'vue-clipboard2'
import '@/assets/js/webim.config'
// require('./assets/js/webim.config')
let WebIM = require('easemob-websdk')



Vue.prototype.$webim = WebIM

const conn = new WebIM.connection({
  isMultiLoginSessions: WebIM.config.isMultiLoginSessions,
  https: typeof WebIM.config.https === 'boolean' ? WebIM.config.https : location.protocol === 'https:',
  url: WebIM.config.xmppURL,
  heartBeatWait: WebIM.config.heartBeatWait,
  autoReconnectNumMax: WebIM.config.autoReconnectNumMax,
  autoReconnectInterval: WebIM.config.autoReconnectInterval,
  apiUrl: WebIM.config.apiURL,
  isAutoLogin: true
})
// 这里使用你的用户名和密码 就ok了
const options = {
  apiUrl: WebIM.config.apiURL,
  user: '',
  pwd: '123456',
  appKey: WebIM.config.appkey,
  success:function (re) {
    console.log('链接服务器正常')
  },
  error:function (err) {
    console.log(err)
  }
}
Vue.prototype.$imconn = conn
Vue.prototype.$imoption = options
Vue.use(Vant)
Vue.use(mui)
Vue.use(CONFIG)
Vue.use(VueClipboard)
Vue.config.productionTip = false
Vue.prototype.$qs = Qs
Vue.prototype.$wxLogin = wxLogin


if(localStorage.getItem('token')){
  store.commit('set_token',localStorage.getItem('token'))
}
if(localStorage.getItem('guidePath')){
  store.commit('set_guide',localStorage.getItem('guidePath'))
}

//注册全局钩子用来拦截导航
router.beforeEach((to, from, next) => {
  if(to.name == 'guide' && store.state.guidePath){
    next({
      path: '/index',
    })
  }
  //获取store里面的token
  let token = store.state.token;
  //判断要去的路由有没有requiresAuth
  if(to.meta.requiresAuth){
   if(token){
    next();
   }else{
    next({
     path: '/',
     query: { redirect: to.fullPath } // 将刚刚要去的路由path（却无权限）作为参数，方便登录成功后直接跳转到该路由
    });
   }
  }else{
   next();//如果无需token,那么随它去吧
  }
 });

new Vue({
  el: '#app',
  router,
  store,//使用store
  components: { App },
  template: '<App/>'
})
